package com.wanglibao.woodtrade.woodtrade.ui.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.SupplyEntity;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.activity.BuyActivity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.adapter.ItemLvSupplyAndDemendAdapter;
import com.wanglibao.woodtrade.woodtrade.util.PullUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshBase;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class SupplyFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {
    @Bind(R.id.lv_supply_fragment)
    PullToRefreshListView mLvSupplyFragment;
    //=====================================================
    JSONObject jsonObject = null;
    SupplyEntity mSupplyEntity = null;
    ItemLvSupplyAndDemendAdapter mAdapter;
    List<SupplyEntity.ListEntity> mListEntity;
    int PageNumber = 1;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.arg2 == 2) {
                if (PageNumber == 1) {
                    if (mSupplyEntity != null && mListEntity != null) {
                        mListEntity.clear();
                    }
                    mAdapter.setList(mListEntity);
                } else {
                    ToastUtil.createLongToast(getContext(), "没有更多数据了");
                    PageNumber--;
                }
            } else {
                SupplyEntity mSupplyEntity1 = new Gson().fromJson(msg.obj.toString(), SupplyEntity.class);
                if (PageNumber == 1) {
                    mSupplyEntity = mSupplyEntity1;
                    mListEntity = mSupplyEntity1.getList();
                } else {
                    mListEntity.addAll(mSupplyEntity1.getList());
                }
                mAdapter.setList(mListEntity);
            }
            mLvSupplyFragment.onRefreshComplete();

            return false;
        }
    });

    public SupplyFragment() {

    }

    boolean isVisible;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint() && isVisible()) {
            isVisible = true;
        } else {
            isVisible = false;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_supply, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        PageNumber = 1;
        NetWorking();
    }

    private void initView() {
        //通用 初始化
        mListEntity = new ArrayList<>();
        mAdapter = new ItemLvSupplyAndDemendAdapter(getContext(), mListEntity);
        mLvSupplyFragment.getRefreshableView().setAdapter(mAdapter);
        mLvSupplyFragment.getRefreshableView().setOnItemClickListener(this);
        mLvSupplyFragment.setMode(PullToRefreshBase.Mode.BOTH);
        mLvSupplyFragment.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
        mLvSupplyFragment.setOnItemClickListener(this);
        mLvSupplyFragment.setOnRefreshListener(this);
        PullUtil.initIndicator(mLvSupplyFragment);
    }

    public void NetWorking() {
        jsonObject = new JSONObject();
        try {
            jsonObject.put("type", 0);
            jsonObject.put("category_one", 0);
            jsonObject.put("category", 0);
            jsonObject.put("num", 0);
            jsonObject.put("page", PageNumber);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (jsonObject != null) {
            JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(false, this, NetConfig.SERVER + NetConfig.TYPE_MESSAGE,
                    NetConfig.messageSendList, mHandler,
                    "jsonObject", NetConfig.TaskId.messageSendList, new Object[]{jsonObject});
            jsonRPCAsyncTask.execute(new Void[]{});
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        PageNumber = 1;
        NetWorking();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        PageNumber = 1;
        NetWorking();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getContext(), BuyActivity.class);
        intent.putExtra("ID", mListEntity.get(position - 1).getId());
        startActivity(intent);
    }
}
